$(document).ready(function(){
	
	$('#admin-win-bar-open').click(function () {
		$('#admin-top-loader').show();
		$('.admin-panel').show();
		loadMenuPages();
		loadAdminPage();
		$('#admin-top-loader').hide();
	});
	
});

function viewAdminSubMenu(dom){
	if (dom.parent().children('ul').css('display') == 'none') {
		dom.parent().children('ul').show();
		dom.css('background-image', 'url(/img/ico/admin/arrow-down.png)');
	} else {
		dom.parent().children('ul').hide();
		dom.css('background-image', 'url(/img/ico/admin/arrow-r.png)');
	}
}

/*
 * load action on li for menu-r
 * */
function actionsMenuRight () {
	$('.admin-menu-r li span:not(.processed)').each(function (){
		$(this).addClass('processed');
		$(this).click(function (){
			loadAdminSubMenu($(this));
		});
	});
}

/*
 * load sub menu-r
 * */
function loadAdminSubMenu(dom){
	var idParentPage = dom.attr('class').replace('menu-page-', '');
		idParentPage = idParentPage.replace('menu-arrow ', '');
		
		$.ajax({
		type:'POST',
		data: {idParentPage: idParentPage},
		url:'/ajax/core/admin/subMenuRight.php',
		success:function (data) {
			if (data != ''){
				dom.parent().append(data);
				dom.css('background-image', 'url(/img/ico/admin/arrow-down.png)');
				actionsMenuRight ();
				dom.unbind('click');
				dom.click(function () {viewAdminSubMenu($(this));});
			} else {
				dom.unbind('click');
				dom.css('background-image', 'none');
			}	
		}
	});
}

/*
 * loader page left
 */
function loadAdminPage () {
	$.ajax({
		type:'POST',
		data: {label: labelPage},
		url:'/ajax/core/admin/adminPage.php',
		success:function (data) {
			$('.admin-panel .col-l').html(data);
		}
	});
}

/*
 * loader menu page right
 */
function loadMenuPages () {
	$.ajax({
		type:'POST',
		data:{idPage: idPage},
		url:'/ajax/core/admin/menuRight.php',
		success:function (data) {
			$('.admin-panel .col-r').html(data);
			loadActionNewPage();
		}
	});
}

function loadActionNewPage () {
	$("#new-page").click(function (){
		addNewPage();
	});
	actionsMenuRight();
}

function addNewPage () {
	parentPage = $('select[name|="parent-page"]').val();
	namePage = $('input[name|="new-page"]').val();
	if (namePage != '') {
		$.ajax({
			type:'POST',
			data:{idParent: parentPage, name: namePage},
			url:'/ajax/core/admin/addNewPage.php',
			success:function (data) {
				eval(data);
			}
		});
	}
}